from django.db import models
from django.utils import timezone


# Create your models here.
# 自定义微小整型字段
class CustomeTinyIntField(models.Field):
    def __init__(self, max_length, *args, **kwargs):
        self.max_length = max_length
        super(CustomeTinyIntField, self).__init__(max_length=max_length, *args, **kwargs)

    def db_type(self, connection):
        return 'tinyint(%s)' % self.max_length


# 自定义char字段
class CustomCharField(models.Field):
    def __init__(self, max_length, *args, **kwargs):
        self.max_length = max_length
        super(CustomCharField, self).__init__(*args, **kwargs)

    def db_type(self, connection):
        return 'char(%s)' % self.max_length


class Manager(models.Model):
    account = models.CharField(max_length=12, default='', unique=True, verbose_name='账号')
    nick_name = models.CharField(max_length=10, default='', unique=True, verbose_name='昵称')
    cipher = models.CharField(max_length=12, default='', verbose_name='密码')
    power = CustomeTinyIntField(max_length = 1,default='0', verbose_name='管理员权限')
    add_time = models.DateTimeField(auto_now_add=True , verbose_name='添加时间')
    state = models.BooleanField(default='1', verbose_name='管理员状态，1正常。2禁用')
    login_time = models.DateTimeField(default=timezone.now(), verbose_name='登录时间')
    login_ip = models.GenericIPAddressField(protocol='both', verbose_name='登录ip')
    login_num = models.PositiveIntegerField(verbose_name='登录次数', default=0)

    class Meta:
        db_table = 'gj_manager'
        verbose_name = '管理员表'
        ordering = ['id']


class Manager_log(models.Model):
    type = models.CharField(max_length=10, verbose_name='操作类型')
    title = models.CharField(max_length=150, verbose_name='操作日志标题')
    add_time = models.DateTimeField(default=timezone.now, verbose_name='操作时间')
    user_agent = models.CharField(max_length=240, verbose_name='记录用户客户端信息')
    referer = models.CharField(max_length=100, verbose_name='操作来源页')
    ip = models.CharField(max_length=15, verbose_name='ip')
    admin = models.ForeignKey(to=Manager, verbose_name='管理员模型的外键', on_delete=models.DO_NOTHING)

    class Meta:
        db_table = 'manage_log'
        ordering = ['id']
        verbose_name = '管理员日志表'
